package cn.sdormitory.workflow.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.sdormitory.basedata.dao.BClassDao;
import cn.sdormitory.basedata.dao.BStudentDao;
import cn.sdormitory.basedata.entity.BClass;
import cn.sdormitory.basedata.entity.BStudent;
import cn.sdormitory.common.utils.SysUserUtils;
import cn.sdormitory.sys.dao.SysUserDao;
import cn.sdormitory.sys.entity.SysUser;
import cn.sdormitory.workflow.dao.HygieneRectificationApplyDao;
import cn.sdormitory.workflow.entity.HygieneRectificationApply;
import cn.sdormitory.workflow.entity.ViolationApply;
import cn.sdormitory.workflow.service.HygieneRectificationApplyService;
import cn.sdormitory.workflow.service.WorkFlowCommonService;
import cn.sdormitory.workflow.vo.WorkFlowVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: lx
 * @date: 2023/10/11 9:45
 * @description:
 */
@Service
@Slf4j
public class HygieneRectificationApplyServiceImpl
        extends ServiceImpl<HygieneRectificationApplyDao, HygieneRectificationApply>
        implements HygieneRectificationApplyService {

    @Autowired
    HygieneRectificationApplyDao rectificationApplyDao;

    @Autowired
    private WorkFlowCommonService workFlowCommonService;

    @Resource
    BStudentDao bStudentDao;

    @Resource
    BClassDao bClassDao;

    @Resource
    SysUserDao sysUserDao;

    @Override
    public int createApply(HygieneRectificationApply vo) {
        String username = SysUserUtils.getSysUser().getUsername();
        if(StringUtils.isEmpty(username)){
            //createBys实际为当前登录用户
            username = vo.getCreateBy();
        }
        //查询审批人--提交时默认审批人为学生的班主任
        if(StringUtils.isEmpty(vo.getStudentId())){
            log.info("学生ID为空");
            return -1;
        }

        LambdaQueryWrapper<HygieneRectificationApply> applyWrapper = new LambdaQueryWrapper<>();
        applyWrapper.eq(HygieneRectificationApply::getStudentId,vo.getStudentId());
        applyWrapper.eq(HygieneRectificationApply::getCheckDate,vo.getCheckDate());
        Integer integer = this.baseMapper.selectCount(applyWrapper);
        if(integer > 0){
            return -2;
        }

        //获取到班主任ID
        BStudent bStudent = bStudentDao.selectById(vo.getStudentId());
        BClass bClass = bClassDao.selectById(bStudent.getClassId());
        Long teacherId = bClass.getClassTeacherId();
        SysUser classTeacher = sysUserDao.selectById(teacherId);

        String applyNo = DateUtil.format(new Date(),"yyyyMMddHHmmssSSS");

        WorkFlowVO flowVO = new WorkFlowVO();
        flowVO.setApplyTitle("宿舍卫生整改处理流程");
        flowVO.setApplyID(vo.getApplyId());
        flowVO.setBusinessKey("HR-" + applyNo);
        Map<String,Object> map =new HashMap<>();
        map.put("assignee",classTeacher.getUsername());
        flowVO.setVariables(map);
        String processInstanceId = workFlowCommonService.createProcessInstance(flowVO);

        vo.setProcessInstanceId(processInstanceId);
        vo.setStudentName(bStudent.getStudentName());
        vo.setApplyNo("HR-" + applyNo);
        if(!StringUtils.isEmpty(username)){
            vo.setCreateBy(username);
        }
        vo.setApplyStatus("1");
        int insert = this.baseMapper.insert(vo);
        return insert;
    }

    @Override
    public IPage<HygieneRectificationApply> pageList(HygieneRectificationApply vo) {
        IPage<HygieneRectificationApply> page = new Page<>(vo.getPageNum(), vo.getPageSize());
        String username = SysUserUtils.getSysUser().getUsername();
        if(StringUtils.isEmpty(username)){
            //createBys实际为当前登录用户
            username = vo.getCreateBy();
        }
        //发起人
        if("1".equals(vo.getType())){
            vo.setCreateBy(username);
        }
        if("2".equals(vo.getType())){
            vo.setCurrentApprover(username);
        }
        return this.baseMapper.getApplyPage(vo,page);
    }

    @Override
    public boolean cancelApply(HygieneRectificationApply vo) {
        LambdaQueryWrapper<HygieneRectificationApply> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HygieneRectificationApply::getProcessInstanceId,vo.getProcessInstanceId());
        HygieneRectificationApply applyVO = new HygieneRectificationApply();
        applyVO.setApplyStatus("5");//撤销流程
        applyVO.setModifyBy(SysUserUtils.getSysUser().getUsername());
        applyVO.setModifyTime(new Date());
        this.baseMapper.update(applyVO,wrapper);
        return workFlowCommonService.cancelApply(vo.getProcessInstanceId(),"");
    }

    @Override
    public void setApprover(WorkFlowVO vo) {
        LambdaQueryWrapper<HygieneRectificationApply> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HygieneRectificationApply::getProcessInstanceId, vo.getProcessInstanceId());
        HygieneRectificationApply rectificationApply = this.baseMapper.selectOne(wrapper);
        Map<String,Object> map = new HashMap<>();
        if(vo.getCurrNode().startsWith("classTeacherHandle")){
            if(0 == vo.getFlag()){
                map.put("assignee",vo.getCreateBy());
                vo.setVariables(map);
            }else if("1".equals(vo.getFlag())){

            }
        }
        if(vo.getCurrNode().startsWith("dorTeacherHandler")){
            if("0".equals(vo.getFlag())){

            }else if(1 == vo.getFlag()){
                BStudent bStudent = bStudentDao.selectById(rectificationApply.getStudentId());
                BClass bClass = bClassDao.selectById(bStudent.getClassId());
                Long teacherId = bClass.getClassTeacherId();
                SysUser classTeacher = sysUserDao.selectById(teacherId);
                map.put("assignee",classTeacher);
                vo.setVariables(map);
            }
        }
    }

    @Override
    public void processResult(WorkFlowVO vo) {
        LambdaQueryWrapper<HygieneRectificationApply> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HygieneRectificationApply::getProcessInstanceId, vo.getProcessInstanceId());
        HygieneRectificationApply rectificationApply = this.baseMapper.selectOne(wrapper);
        Map<String,Object> map = new HashMap<>();
        if(vo.getCurrNode().startsWith("classTeacherHandle")){
            if(0 == vo.getFlag()){
                rectificationApply.setApplyStatus("2");
                this.baseMapper.updateById(rectificationApply);
            }else if(1 == vo.getFlag()){
                rectificationApply.setApplyStatus("3");
                this.baseMapper.updateById(rectificationApply);
            }
        }
        if(vo.getCurrNode().startsWith("dorTeacherHandler")){
            if("0".equals(vo.getFlag())){
                rectificationApply.setApplyStatus("4");
                this.baseMapper.updateById(rectificationApply);
            }else if(1 == vo.getFlag()){
                rectificationApply.setApplyStatus("1");
                this.baseMapper.updateById(rectificationApply);
            }
        }
    }

}
